package com.bot.pms.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bot.pms.common.domain.Message;
import com.bot.pms.project.domain.base.PageQueryResult;
import com.bot.pms.project.domain.bo.ProjectBo;
import com.bot.pms.project.domain.bo.ProjectPageQuery;
import com.bot.pms.project.domain.bo.ProjectPageQueryResultBo;
import com.bot.pms.project.domain.entity.Project;
import com.bot.pms.project.domain.vo.ProjectPageQueryResultVo;
import com.bot.pms.project.domain.vo.ProjectVo;
import com.bot.pms.project.enums.MessageStatusCodeEnum;
import com.bot.pms.project.enums.ProjectStatusEnum;
import com.bot.pms.project.mapper.ProjectMapper;
import com.bot.pms.project.service.ProjectService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;

/**
 * @Author: tian
 * @CreateTime: 2024-12-07
 * @Description: 项目服务的实现
 * @Version: 1.0
 */
@Service
public class ProjectServiceImpl implements ProjectService {

    @Resource
    private ProjectMapper projectMapper;

    @Override
    public Message addProject(ProjectBo projectBo) {
        Message message = new Message();
        //1. 处理项目基本属性
        Project project = new Project();
        BeanUtil.copyProperties(projectBo,project);
        //2. 维护系统属性
        project.setStatus(ProjectStatusEnum.DRAFT.getValue());
        //3. 插入
        boolean isOK = projectMapper.insert(project);
        //4. 处理消息
        if(isOK){
            message.setStatusCode(MessageStatusCodeEnum.SUCCESS.getValue());
            message.setStatusDes("OK");
        }else {
            message.setStatusCode(MessageStatusCodeEnum.SERVER_ERROR.getValue());
            message.setStatusDes("服务器错误");
        }

        return message;
    }

    @Override
    public Message pageQuery(ProjectPageQuery projectPageQuery) {
        Message message = new Message();

        // 查询总条数
        int totalNum = projectMapper.pageQueryTotalNum(projectPageQuery);
        // 分页条件查询
        PageHelper.startPage(projectPageQuery.getPageNo(), projectPageQuery.getPageSize());
        ArrayList<ProjectPageQueryResultBo> results = projectMapper.pageQuery(projectPageQuery);
        // 处理为Vo(日期)
        ArrayList<ProjectPageQueryResultVo> resultVos = new ArrayList<>();
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for (ProjectPageQueryResultBo projectPageQueryResultBo : results) {
            ProjectPageQueryResultVo projectPageQueryResultVo = new ProjectPageQueryResultVo();
            BeanUtil.copyProperties(projectPageQueryResultBo,projectPageQueryResultVo);
            projectPageQueryResultVo.setPreStartDate(projectPageQueryResultBo.getPreStartDate().format(dateFormatter));
            projectPageQueryResultVo.setPreFinishDate(projectPageQueryResultBo.getPreFinishDate().format(dateFormatter));
            resultVos.add(projectPageQueryResultVo);
        }
        // 组装结果
        PageQueryResult<ProjectPageQueryResultVo> pageQueryResult = new PageQueryResult<>();
        pageQueryResult.setTotalNum(totalNum);
        int pageNo = projectPageQuery.getPageNo();
        pageQueryResult.setPageNo(pageNo);
        int pageSize = projectPageQuery.getPageSize();
        pageQueryResult.setPageSize(pageSize);
        pageQueryResult.setPageNum((totalNum - 1) / pageSize + 1);
        pageQueryResult.setResults(resultVos);
        // 处理消息
        message.setStatusCode(MessageStatusCodeEnum.SUCCESS.getValue());
        message.setStatusDes("OK");
        message.setData(pageQueryResult);

        return message;
    }

    /**
     * 查看详情
     * @param id
     * @return
     */
    @Override
    public Message viewDetail(Long id) {
        Message message = new Message();

        // 查询
        Project project = projectMapper.selectById(id);
        ProjectVo projectVo = new ProjectVo();
        BeanUtil.copyProperties(project,projectVo);
        // 转换日期和日期时间
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String preFinishDate = project.getPreFinishDate().format(dateFormatter);
        String preStartDate = project.getPreStartDate().format(dateFormatter);
        projectVo.setPreFinishDate(preFinishDate);
        projectVo.setPreStartDate(preStartDate);
        if(null != project.getStartTime()){
            String startTime = project.getStartTime().format(dateTimeFormatter);
            projectVo.setStartTime(startTime);
        }
        if(null != project.getFinishTime()){
            String finishTime = project.getFinishTime().format(dateTimeFormatter);
            projectVo.setFinishTime(finishTime);
        }
        // 处理消息
        message.setStatusCode(MessageStatusCodeEnum.SUCCESS.getValue());
        message.setStatusDes("OK");
        message.setData(projectVo);

        return message;
    }

    @Override
    public Message edit(ProjectBo projectBo) {
        Message message = new Message();

        Project project = new Project();
        BeanUtil.copyProperties(projectBo,project);
        //3. 插入
        boolean isOK = projectMapper.updateById(project);
        //4. 处理消息
        if(isOK){
            message.setStatusCode(MessageStatusCodeEnum.SUCCESS.getValue());
            message.setStatusDes("OK");
        }else {
            message.setStatusCode(MessageStatusCodeEnum.SERVER_ERROR.getValue());
            message.setStatusDes("服务器错误");
        }

        return message;
    }
}
